﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.BusinessRules;
using System.IO;
using FrienDev.BusinessRules.ThumbnailHelper;
using FrienDev.Data;

public partial class Controls_Activity_ImageAdd : System.Web.UI.UserControl
{
    private int m_activityId;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Request.QueryString["activityid"]))
        {
            m_activityId = Convert.ToInt32(Request.QueryString["activityid"]);
            hl_AllImage.NavigateUrl = "~/Activity/ImageListDetail.aspx?activityid=" + m_activityId;
            bt_UploadFile.Click += new EventHandler(bt_UploadFile_Click);
            if (ActivityRules.Instance.IsUserJoinedActivity(m_activityId, UserRules.Instance.GetCurrentUserId()))
            {
                pn_Privacy.Visible = false;
                pn_UploadImage.Visible = true;
            }
            else
            {
                pn_Privacy.Visible = true;
                pn_UploadImage.Visible = false;
            }
        }
    }

    void bt_UploadFile_Click(object sender, EventArgs e)
    {
        string fileExt = System.IO.Path.GetExtension(up_Photo.FileName).ToLower();
        if (up_Photo.HasFile && (up_Photo.FileBytes.Length < 2048000) && fileExt == ".png" || fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp")
        {
            string bpath = Server.MapPath("~\\Upload\\ActivityPhoto\\" + m_activityId + "\\back\\");
            string spath = Server.MapPath("~\\Upload\\ActivityPhoto\\" + m_activityId + "\\s\\");
            string mpath = Server.MapPath("~\\Upload\\ActivityPhoto\\" + m_activityId + "\\m\\");
            if (!Directory.Exists(bpath))
            {
                Directory.CreateDirectory(bpath);
            }
            if (!Directory.Exists(spath))
            {
                Directory.CreateDirectory(spath);
            }
            if (!Directory.Exists(mpath))
            {
                Directory.CreateDirectory(mpath);
            }
            int photonum = PhotoRules.Instance.GetActivityLastestPhotoId(m_activityId) + 1;
            string fileName = m_activityId + "_" + DateTime.Now.ToLocalTime().ToString("yyMMdd") + "_" + photonum + fileExt;
            try
            {
                this.up_Photo.SaveAs(bpath + fileName);
                ThumbnailGenerater.CreateNewThumbnailImage(bpath
                        , fileName
                        , spath
                        , fileName
                        , 127
                        , 95
                        , false
                        , "");
                ThumbnailGenerater.CreateNewThumbnailImage(bpath
                        , fileName
                        , mpath
                        , fileName
                        , 420
                        , 315
                        , false
                        , "");
                PhotoRules.Instance.InsertActivityPhoto(m_activityId, fileName, txt_Description.Text.Trim(), UserRules.Instance.GetCurrentUserId());
                txt_Description.Text = "";
                Response.Redirect("~/Activity/ImageListDetail.aspx?activityid=" + m_activityId);
            }
            catch
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('上传照片失败');", true);
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "myscript", "javascript:alert('文件类型不正确或文件大于2M');", true);
        }
    }
}
